﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace jnpic.Web
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Cookies["UserInfo"] != null)
                    {
                        string name = Request.Cookies["UserInfo"]["LoginName"];
                        string pwd = Request.Cookies["UserInfo"]["Password"];
                        name = jnpic.Common.DEncrypt.DESEncrypt.Decrypt(name);
                        pwd = jnpic.Common.DEncrypt.DESEncrypt.Decrypt(pwd);
                        jnpic.BLL.userinfo u = new BLL.userinfo();
                        DataSet um = u.Login(name, pwd);
                        if (um.Tables[0].Rows.Count == 1)
                        {
                            jnpic.Model.userinfo userModel = u.Login(name, pwd, 1);
                            Session["user"] = um.Tables[0];
                            Session["UserInfo"] = userModel;                           
                            Response.Redirect("Main.aspx");
                        }

                    }
            }

        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string name = txtName.Text;
            string pwd = txtPwd.Text;
            jnpic.BLL.userinfo u = new BLL.userinfo();
            DataSet um = u.Login(name,pwd);
            if (um.Tables[0].Rows.Count == 1)
            {
                jnpic.Model.userinfo userModel = u.Login(name, pwd, 1);
                Session["user"] = um.Tables[0];
                Session["UserInfo"] = userModel;
                if (chkpwd.Checked)
                {
                    HttpCookie userCookie = new HttpCookie("UserInfo");
                    userCookie.Expires = DateTime.Now.AddDays(15);
                    userCookie["UserID"] = jnpic.Common.DEncrypt.DESEncrypt.Encrypt(userModel.uid.ToString());
                    userCookie["DisplayName"] = jnpic.Common.DEncrypt.DESEncrypt.Encrypt(userModel.utruename);
                    userCookie["LoginName"] = jnpic.Common.DEncrypt.DESEncrypt.Encrypt(userModel.uname);
                    userCookie["Password"] = jnpic.Common.DEncrypt.DESEncrypt.Encrypt(userModel.upwd);
                    userCookie["Remark"] = jnpic.Common.DEncrypt.DESEncrypt.Encrypt(userModel.remark);
                    Response.Cookies.Add(userCookie);
                }                
                Response.Redirect("Main.aspx");
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "a", "alert('用户名或密码错误！请重新登录！')", true);
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "alert('用户名或密码错误！请重新登录！')", true);
            }
        }
    }
}